package com.loop.unclelend.ui.fragment

import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPageMenuBinding
import com.loop.unclelend.pro.ProFragment
import com.loop.unclelend.ui.activity.Act_Feedback
import com.loop.unclelend.ui.activity.Act_Login
import com.loop.unclelend.ui.activity.Act_Steps
import com.loop.unclelend.ui.adapter.Adapter_Menu
import com.loop.unclelend.ui.presenter.Pre_Menu
import com.loop.unclelend.ui.view.IView_Menu
import com.loop.unclelend.utils.adapter.UncleOnItemChildClickListener
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.helper.CustomProcessHelper
import com.loop.unclelend.utils.helper.SharedHelper
import com.loop.unclelend.utils.helper.TestDataLibInvoke
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/9 16:49
 * @Annotation：侧边菜单
 */
class Fm_Menu(): ProFragment(), IView_Menu {
    lateinit var binding: UiPageMenuBinding
    private var drawerLayout: DrawerLayout? = null
    var presenter: Pre_Menu? = null

    //菜单列表
    var menuAdapter: Adapter_Menu? = null

    constructor(drawerLayout: DrawerLayout): this(){
        this.drawerLayout = drawerLayout
    }
    override fun getUncleRootView(inflater: LayoutInflater): View {
        binding = UiPageMenuBinding.inflate(inflater)
        return binding.root
    }

    override fun initUncleView() {
        rootViewBinding.layoutRoot.setBackgroundColor(Color.parseColor("#00000000"))
        initRecyclerView()
    }

    override fun initUncleListener() {
        //关闭
        binding.layoutClose.setOnClickListener {
            if (drawerLayout != null){
                drawerLayout!!.closeDrawer(GravityCompat.START)
            }
        }
        binding.layoutContent.setOnClickListener {
            return@setOnClickListener
        }
    }

    override fun initUncleData() {
        presenter = Pre_Menu(this)
        presenter!!.loadUncleMenuList()
    }

    /**
     * 初始化菜单列表
     */
    private fun initRecyclerView() {
        binding.rvMenu.layoutManager = LinearLayoutManager(getAct())
        menuAdapter = Adapter_Menu(binding.rvMenu)
        binding.rvMenu.adapter = menuAdapter
        binding.rvMenu.itemAnimator = DefaultItemAnimator()
        menuAdapter!!.setOnItemChildClickListener(UncleOnItemChildClickListener { _, childView, position ->
            val item = menuAdapter!!.getItem(position)
            if (childView.id == R.id.layout_item) {//切换item
                when(item.optInt("menuId")){
                    0 -> {//Contact us
                        showUncleKefuDialog("User Center")
                    }
                    1 -> {//Information
                        showUncleSteps()
                    }
                    2 -> {//Feedback
                        binding.layoutClose.callOnClick()
                        Handler(Looper.getMainLooper()).postDelayed({
                            onIntent(Act_Feedback::class.java)
                        },300)
                    }
                    3 -> {//Language
                        showUncleToast("Language")
                    }
                    4 -> {//Log Out
                        onLoginOutClick()
                    }
                }
            }
        })
    }

    override fun showUncleMenuList(menuList: ArrayList<JSONObject>) {
        menuAdapter!!.data = menuList
    }

    /**
     * 跳转进件信息
     */
    private fun showUncleSteps(){
        if (CustomProcessHelper.getUncleStepList().size == 0) {//流程已完成 - 提示
            showErrorTipsDialog(getString(R.string.menu_profile_complete_tips),"Menu Click Information")
        } else {
            //流程未完成 - 进入流程页面
            binding.layoutClose.callOnClick()
            Handler(Looper.getMainLooper()).postDelayed({
                //上报
                TestDataLibInvoke.sycData()
                onIntent(Act_Steps::class.java)
            },300)
        }
    }

    /**
     * 退出登录
     */
    fun onLoginOutClick(){
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_USER_LOGINOUT,"")
        //退出登录，需要移除用户保存的信息
        SharedHelper.onUncleLoginOut()
        Handler(Looper.getMainLooper()).postDelayed({
            onIntent(Act_Login::class.java)
        },300)
    }
}